<template>
  <div class="app-container-sm">
      <el-card class="card-margin-bottom">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="form-minus-bottom">
      <base-select v-model="queryParams.baseId"></base-select>
      <el-form-item label="溯源产品" prop="sellproId">
        <el-select v-model="queryParams.sellproId" placeholder="请选择溯源产品" clearable size="medium">
          <el-option
            v-for="item in sellproList"
            :key="item.sellproId"
            :label="item.sellproName"
            :value="item.sellproId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
      </el-card>
<el-card class="card-padding-bottom">
    <el-alert class="margin-bottom-20" title="溯源码较多时下载图片需要耐心等待，1000个溯源码下载时间约为30-50秒" type="warning" effect="light" show-icon closable></el-alert>

    <el-table v-loading="loading" :data="versionList" >
      <!-- <el-table-column label="溯源版本ID" align="center" prop="sellproId" width="200px"/> -->
      <el-table-column label="溯源产品名称" align="center" prop="sellproName" width="200px"/>
      <el-table-column label="溯源码范围" align="center" prop="codeRange">
          <template #default="{row}">
              <div style="opacity:0.5" class="font-style-italic background-color-l3 font-color-fff padding-1 font-weight-bold border-radius-3">{{row.codeRange}}</div>
          </template>
      </el-table-column>
      <el-table-column label="已溯源/总数" align="center" prop="useDate" width="120px">
      <template #default="{row}">
          <span class="font-color-primary">{{row.useDate}}</span>
      </template>
      </el-table-column>
      <el-table-column label="生成时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="380px">
        <template slot-scope="scope">
            <el-button
            size="small"
            type="primary"
            class="padding-5"
            icon="el-icon-picture"
            @click="handleDownload(scope.row)"
            v-hasPermi="['trace:code:download']"
          >溯源码下载</el-button>
          <el-button
            size="small"
            type="warning"
            class="padding-5"
            icon="el-icon-download"
            @click="handleExport(scope.row)"
            v-hasPermi="['trace:code:export']"
          >溯源码导出</el-button>
          <el-button
            size="small"
            type="info"
            plain
            class="padding-5"
            icon="el-icon-setting"
            @click="handleCode(scope.row)"
            v-hasPermi="['trace:version:code']"
          >溯源码查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
</el-card>
  </div>
</template>

<script>
import { listVersion} from "@/api/trace/version";
import { listSellpro } from "@/api/trace/sellpro";

export default {
  name: "",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 溯源版本表格数据
      versionList: [],
      sellproList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sellproId: null,
        baseId:null
      }
    };
  },
  async created() {
    await this.getSellproList();
    this.queryParams.sellproId =this.$route.query.sellproId? Number(this.$route.query.sellproId):null;
    this.getList();
  },
  methods: {
    /** 查询溯源版本列表 */
    getList() {
      this.loading = true;
      listVersion(this.queryParams).then(response => {
        this.versionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    async getSellproList(){
        const { rows } = await listSellpro();
        this.sellproList = rows;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 溯源码按钮操作 */
    handleCode(row){
        this.$router.push({
            path: '/trace/code',
            query: {
                t: Date.now(),
                versionId:row.versionId,
                sellproId:row.sellproId
            }
        });
    },
    /** 溯源码下载 */
    handleDownload(row){
        this.download('trace/code/download', {
          //导出所有未被溯源的数据
        versionId:row.versionId,status:0
      }, `溯源码_${row.codeRange}_${new Date().getTime()}.zip`)
    },
    /** 溯源码导出 */
    handleExport(row) {
      this.download('trace/code/export', {
          //导出所有未被溯源的数据
        versionId:row.versionId,status:0
      }, `溯源码_${row.codeRange}_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
